﻿using Caliburn.Micro;
using myapp.Databases.DAO;
using myapp.Events;
using myapp.Events.ViewModel;
using myapp.Instances;
using myappcore.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myapp.ViewModels
{
    /// <summary>
    /// Item List ViewModel
    /// </summary>
    public class ItemListViewModel : IBaseContentViewModel, IHandle<ViewModelItemGroupSelectedEvent>
    {
        #region Attribute
        private ObservableCollection<ItemViewModel> _ItemViewList;

        private static ItemListViewModel _instance;
        #endregion

        #region Properties

        /// <summary>
        /// Group of Item
        /// </summary>
        public ItemGroup ItemGroup { get; set; }

        /// <summary>
        /// List Item of Group
        /// </summary>
        public ObservableCollection<ItemViewModel> ItemViewList
        {
            get
            {
                if (null == _ItemViewList)
                {
                    _ItemViewList = new ObservableCollection<ItemViewModel>();
                }
                if (IsChanged)
                {
                    _ItemViewList.Clear();
                    if (null != ItemGroup)
                    {
                        foreach (Item item in ItemDao.ItemOfGroup(ItemGroup))
                        {
                            ItemViewModel itemViewModel = new ItemViewModel()
                            {
                                Item = item,
                                CurrentColor = MainInstance.Instance.ColorConfig.ItemNormal
                            };
                            _ItemViewList.Add(itemViewModel);
                        }
                    }
                    if (_ItemViewList.Count > 0)
                    {
                        IsChanged = false;
                    }
                }
                return _ItemViewList;
            }
        }

        public static ItemListViewModel Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new ItemListViewModel();
                    _instance.IsChanged = true;
                }
                return _instance;
            }
        }
        #endregion
        private ItemListViewModel()
        {
            MainInstance.Instance.EventAggregator.Subscribe(this);
            DisplayName = "Danh sách món";
        }
        #region Constructure


        #endregion

        #region Method

        /// <summary>
        /// Handle ViewModelItemGroupSelectedEvent
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelItemGroupSelectedEvent message)
        {
            ItemGroup = message.ItemGroupViewModel.ItemGroup;
            IsChanged = true;
            NotifyOfPropertyChange(() => ItemViewList);
        }
        #endregion


    }
}
